package org.esprit.ocm.server.metier.oca.impl;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.rpc.oca.ImageOcaService;
import org.esprit.ocm.model.oca.ImageOca;
import org.esprit.ocm.server.metier.oca.api.Client;
import org.esprit.ocm.server.metier.oca.api.OneResponse;
import org.esprit.ocm.server.metier.oca.api.image.Image;
import org.esprit.ocm.server.metier.oca.api.image.ImagePool;

public class ImageOcaServiceImpl implements ImageOcaService {

	/**
	 * List All hosts
	 * 
	 * @param client
	 * @return
	 * @throws Exception
	 */
	public List<ImageOca> listAll(String secret, String endpoint, int filter) throws Exception {
		Client client = new Client(secret, endpoint);
		ImagePool pool = new ImagePool(client, filter);
		pool.info();

		List<ImageOca> l = new ArrayList<ImageOca>();

		for (Image from : pool) {
			ImageOca to = new ImageOca();
			to.setId(from.xpath("ID"));
			to.setUid(from.xpath("UID"));
			to.setUsername(from.xpath("USERNAME"));
			to.setName(from.xpath("NAME"));
//			to.setType(from.typeStr());
			to.setType(from.xpath("TYPE"));
			to.setPublicState(from.xpath("PUBLIC"));
			to.setPersistent(from.xpath("PERSISTENT"));
			to.setRegtime(from.xpath("REGTIME"));
			to.setSource(from.xpath("SOURCE"));
			to.setState(from.xpath("STATE"));
//			to.setState(from.stateString());
			to.setRunning_vms(from.xpath("RUNNING_VMS"));

			to.setTemplateOca_dev_prefix(from.xpath("TEMPLATE/DEV_PREFIX"));
			to.setTemplateOca_name(from.xpath("TEMPLATE/NAME"));
			to.setTemplateOca_path(from.xpath("TEMPLATE/PATH"));
			to.setTemplateOca_type(from.xpath("TEMPLATE/TYPE"));

			to.setErrorMSG(from.info().getErrorMessage());

			l.add(to);
		}
		return l;
	}

	/**
	 * Create New Image
	 * 
	 * @param client
	 * @param template
	 * @param image
	 * @return
	 * @throws Exception
	 */
	public String allocate(String secret, String endpoint, String template) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Image.allocate(client, template);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Delete an image
	 * 
	 * @param client
	 * @param imgId
	 * @throws Exception
	 */
	public String delete(String secret, String endpoint, int imgId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Image.delete(client, imgId);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Describe Image information
	 * 
	 * @param client
	 * @param imgId
	 * @return
	 * @throws Exception
	 */
	public ImageOca info(String secret, String endpoint, int imgId) throws Exception {
		Client client = new Client(secret, endpoint);
		Image from = new Image(imgId, client);

		ImageOca to = new ImageOca();
		to.setId(from.xpath("ID"));
		to.setUid(from.xpath("UID"));
		to.setName(from.xpath("NAME"));
		to.setType(from.typeStr());
//		to.setType(from.xpath("TYPE"));
		to.setPublicState(from.xpath("PUBLIC"));
		to.setPersistent(from.xpath("PERSISTENT"));
		to.setRegtime(from.xpath("REGTIME"));
		to.setSource(from.xpath("SOURCE"));
//		to.setState(from.xpath("STATE"));
		to.setState(from.stateString());
		to.setRunning_vms(from.xpath("RUNNING_VMS"));

		to.setTemplateOca_dev_prefix(from.xpath("TEMPLATE/DEV_PREFIX"));
		to.setTemplateOca_name(from.xpath("TEMPLATE/NAME"));
		to.setTemplateOca_path(from.xpath("TEMPLATE/PATH"));
		to.setTemplateOca_type(from.xpath("TEMPLATE/TYPE"));

		to.setErrorMSG(from.info().getErrorMessage());
		return to;
	}

	/**
	 * Enable Image
	 * 
	 * @param client
	 * @param imgId
	 * @return
	 * @throws Exception
	 */
	public String enable(String secret, String endpoint, int imgId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Image.enable(client, imgId, true);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * disable image
	 * 
	 * @param client
	 * @param imgId
	 * @return
	 * @throws Exception
	 */
	public String disable(String secret, String endpoint, int imgId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Image.enable(client, imgId, false);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * publish image
	 * 
	 * @param client
	 * @param imgId
	 * @return
	 * @throws Exception
	 */
	public String publish(String secret, String endpoint, int imgId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Image.publish(client, imgId, true);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * unpublish image
	 * 
	 * @param client
	 * @param imgId
	 * @return
	 * @throws Exception
	 */
	public String unpublish(String secret, String endpoint, int imgId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Image.publish(client, imgId, false);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Remove attribute from image template
	 * 
	 * @param client
	 * @param imgId
	 * @param att_name
	 * @return
	 * @throws Exception
	 */
	public String rmattr(String secret, String endpoint, int imgId, String att_name) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Image.rmattr(client, imgId, att_name);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Update attribute from image template
	 * 
	 * @param client
	 * @param imgId
	 * @param att_name
	 * @param att_val
	 * @return
	 * @throws Exception
	 */
	public String update(String secret, String endpoint, int imgId, String att_name, String att_val) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = Image.update(client, imgId, att_name, att_val);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}
}
